package controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Api(tags = "邮件发送通用接口")
@RestController
public class EmailController {

    @Autowired
    private JavaMailSender jms;

    @Value("${spring.mail.username}")
    private String username;

    @RequestMapping(value = "/SendEmail", method = RequestMethod.POST)
    public String SendEmail(){
        System.out.println("进入发送普通邮件的方法");
        String str=null;
        try {
            //声明邮箱对象
            SimpleMailMessage message = new SimpleMailMessage();
            //通过邮箱对象设置相关参数
            message.setFrom(username);
            message.setTo("");
            message.setSubject("发送一封简单的邮件");
            message.setText("欢迎假期来学习大数据应用技术");
            //发送邮件
            jms.send(message);
            str = "邮件发送成功";
        }catch (Exception e){
            System.out.println("简单邮件发送失败！原因为"+e.getMessage());
            str = "发送失败";
        }
        return str;
    }

    @RequestMapping(value = "/SendHtmlEmail", method = RequestMethod.POST)
    public String SendHtmlEmail(){
        System.out.println("进入发送html内容的邮件方法");
        String str="";
        try {
            MimeMessage message=null;
            message=jms.createMimeMessage();

            MimeMessageHelper html = new MimeMessageHelper(message,true);
            html.setFrom(username);
            html.setTo("");
            html.setSubject("Html的一封邮件");

            //声明带有html命令格式的内容
            StringBuffer sb=new StringBuffer("<p style='color:#42b98e'>使用大数据分析内容,"+
                    "通过html格式将内容发送到网页上。</p>");

            html.setText(sb.toString(),true);

            jms.send(message);

            str="html邮件发送成功！";
        } catch (Exception ex) {
            System.out.println("html邮件发送失败，原因为："+ex.getMessage());
            str="html邮件发送失败";
        }
        return str;
    }

    @RequestMapping(value = "SendFuJian", method = RequestMethod.POST)
    public String SendFuJianEmail(){
        System.out.println("进入发送附件的邮件方法");
        String str="";
        try {
            MimeMessage message=null;
            message = jms.createMimeMessage();
            MimeMessageHelper html = new MimeMessageHelper(message,true,"URF-8");

            html.setFrom(username);
            html.setTo("");
            html.setSubject("发送一个带附件的邮件");
            html.setText("详情请下载附件：");
            FileSystemResource file = new FileSystemResource("src\\main\\resources\\static.file\\测试结果.docx");
            html.addAttachment("测试结果.docx",file);

            jms.send(message);
            str = "发送带附件的邮件成功";
        }catch (Exception e){
            System.out.println("发送附件邮件失败，原因为："+e.getMessage());
            str = "附件发送失败！";
        }
        return str;
    }

    @RequestMapping(value = "SendImgEmail", method = RequestMethod.POST)
    public String SendImgEmail(){
        System.out.println("进入发送带有图片的网页方法");
        String str="";
        try {
            MimeMessage message = null;
            message=jms.createMimeMessage();
            MimeMessageHelper html = new MimeMessageHelper(message,true,"UTF-8");

            html.setFrom(username);
            html.setTo("");
            html.setSubject("发送带有图片的网页");
            String txt = "<html><body>哈喽哈喽<img src='cid:img'/></body></html>";
            FileSystemResource fileImg = new FileSystemResource("src\\main\\resources\\img\\sunshine.png");
            html.addAttachment("sunshine.png",fileImg);

            jms.send(message);
            str="带图片网页的邮件发送成功！";
        }catch (Exception e){
            System.out.println("发送带有图片的网页失败，原因是："+e.getMessage());
            str="发送带有图片的网页失败";
        }
        return str;
    }
}
